<?php	
	//echo $acStudent;
?>
<script type="text/javascript">
var autoId=0;

function addDetail(){
	var url="index.php?r=keuangan/tu/studentInvoice/invoiceDetailForm";
	var winName = "Form Detail Invoice";
	var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
	var width=400,height=300;
	var replace = false,onCenter = true;
	var w = openWindow(winName,url,width,height,onCenter,specs,replace);
	w.focus();
	var timer = setInterval(function() {   
		if(w.closed) {  
			clearInterval(timer); 
			//requeryOutstanding();
		}  
	}, 1000);
}
function doAddDetail(itemId,itemDesc,notes,amount,qty,disc,total){
	autoId+=1;
	data = {id:autoId,student_item_id:itemId,student_item_desc:itemDesc,notes:notes,total:amount,qty:qty,disc:disc,total_after_disc:total,status:'new'};	
	$("#tblInvDetail").jqGrid("addRowData",autoId,data,"first",0);
	recalculateTotal();
}
function deleteRow(id){	
	var data = $("#tblInvDetail").jqGrid("getRowData",id);
	if (data.status =="new"){
		//$("#tblInvDetail").jqGrid("setCell",id,"status","delete");
		$("#tblInvDetail").jqGrid("delRowData",id);
	}else{
		$("#tblInvDetail").jqGrid("setCell",id,"status","deleted");
		// Hide row
		$("#tblInvDetail #" + id).css("display","none");
	}
	recalculateTotal();
}
function save(){
	var data= $("#tblInvDetail").jqGrid("getGridParam","data");	
	$("#invDetail").val(JSON.stringify(data));	
	$("#frmData").submit();
}

function departmentOnChange(){
	requeryTingkatan();	
}

function tingkatanOnChange(){
	requeryKelas();
}

function requeryTingkatan(){
	//var filter = {"is_active":1,"department_id":$("#department_id").val()};
	var data = {"department_id":$("#department_id").val()};
	controler = "data_select/SelectActiveTingkatanByDepartment";
	//alert(controler);	
	var url = "index.php?r=" + controler;
	$.ajax({	
		url : url,
		type:"GET",
		async:false,
		data: data,
		success: function(respData) {
			fillSelectData("#tingkatan_id",respData,false);	
		},
	});
	
	requeryKelas();	
}

function requeryKelas(){	
	var data = {"tingkatan_id":$("#tingkatan_id").val()};
	controler = "data_select/SelectActiveKelasByTingkatan";	
	//alert(controler);
	var url = "index.php?r=" + controler;
	$.ajax({	
		url : url,
		type:"GET",
		async:false,
		data: data,
		success: function(respData) {
			fillSelectData("#kelas_tahun_ajaran_id",respData,true);	
		},
	});	
	getTotalStudent();
}
function recalculateTotal(){
	var ids = jQuery("#tblInvDetail").getDataIDs();
	total = 0;
	disc =0;
	ttlAfterDisc = 0;
	 
	for(var i=0;i<ids.length;i++){
		var data = $("#tblInvDetail").jqGrid("getRowData",ids[i]);
		if (data.status != "deleted"){			
			total += data.total * data.qty;
			disc += data.disc * 1;
		}
	}
	ttlAfterDisc = total - disc;
	$("#total").html(accounting.formatMoney(total));
	$("#disc").html(accounting.formatMoney(disc));
	$("#totalAfterDisc").html(accounting.formatMoney(ttlAfterDisc));	
}

function getTotalStudent(){
	var url = "index.php?r=keuangan/tu/studentInvoice/calculateActiveTotalStudent";
	var data = {"tingkatan_id":$("#tingkatan_id").val(),
			"kelas_tahun_ajaran_id":$("#kelas_tahun_ajaran_id").val(),
			"department_id":$("#department_id").val()};
	$.ajax({	
		url : url,
		type:"GET",
		async:false,
		data: data,
		success: function(respData) {
			alert(respData.ttl_student);
		},
	});	
}


$(function(){
	$("#tblInvDetail").jqGrid({
		//url:'index.php?r=keuangan/tu/studentInvoice/invoiceDetail',
		//editUrl:'index.php?r=akademik/pendaftaran_kelas/kelas_tahun_ajaran/edit',
		postData:{"id":$("#id").val()},
		data:<?php echo $dInvBulk;?>,
		datatype: 'local',
		loadonce:true,
		mtype:"GET",
		autowidth:true,
		height:"100%",
		altRows:true,
		ignoreCase:true,
		colNames: ['id','Item ID','Item Name','Notes','Amount','Qty','Disc','Total','Status','Action'],
		colModel: [
			{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
			{name: 'student_item_id', index: 'student_item_id', width: 40,search:true},
			{name: 'student_item_desc', index: 'student_item_desc',search:true},
			{name: 'notes', index: 'notes', formatter: 'textarea',width:100},
			{name: 'total', index: 'total', width: 40,template:moneyTemplate},
			{name: 'qty', index: 'qty', width: 40,template:moneyTemplate},
			{name: 'disc', index: 'disc', width: 40,template:moneyTemplate},
			{name: 'total_after_disc', index: 'total_after_disc', width: 40,template:moneyTemplate},
			{name: 'status', index: 'status', width: 40},
			{name: 'act', index: 'act', width: 40, align: 'center'},				
		],
		gridview: true,
		rownumbers: false,
		rowNum: 10,
		rowList: [5, 10, 15],
		pager: '#navTblOut',
		viewrecords: true,
		caption: 'Department',
		jsonReader : { 
			root:"model",
			repeatitems: false			
		},
		loadComplete:function(data){
			console.log(data);
		},
		gridComplete: function(){ 
			var ids = jQuery("#tblInvDetail").getDataIDs(); 
			console.log(ids);
			
			for(var i=0;i<ids.length;i++){ 
				var cl = ids[i]; 
				//btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(" + cl + ")' />"; 
				btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(" + cl + ")' />"; 
				jQuery("#tblInvDetail").setRowData(ids[i],{act:btnDelete}) 
			} 
		}
	});	
});	
</script>
<div style="width:800px">
	<div id="tabs">
		<div style="border-bottom:2px solid #000;padding-bottom:5px;margin-bottom:20px">
		<?php 
			$this->widget('application.components.widgets.ActionBar', 
				array("menuAlign" => "right", 
					'items' => array(
						array(
							'id' => "btnRefresh",
							'title' => "Simpan",
							'icon' => "images/action/Actions-document-save-all-icon.png",
							'callback'=>"save"
						),					
						array(
							'id' => "btnCancel",
							'title' => "Batal",
							'icon' => "images/action/Actions-dialog-cancel-icon.png",
							'callback'=>"cancel"
						),
					)
				)
			);
		?>
		</div>
		<form id="frmData" action="index.php?r=keuangan/tu/studentInvoice/editBulk" method="POST">
			<?php
				$labelWidth = "100px";
			?>
			
			<div>
				<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Bulk No :</label>
				<input type="text" id="bulk_no" name="bulk_no" value="<?php echo $bulk_no; ?>" />
				<input id="id" name="id" value="<?php echo $id; ?>" />
				<input id="op" name="op" value="<?php echo $op;?>" />				
			</div>
			
			<div>
				<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Departemen :</label>
				<select name="department_id" id="department_id" onChange="departmentOnChange()">
					<?php
						foreach ($arrDepartment as $department){
							echo "<option value='". $department->id. "'>". $department->id ."</option>";
						} 
					?>
				</select>
			</div>
			<div>
				<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Tingkatan :</label>
				<select name="tingkatan_id" id="tingkatan_id" onChange="tingkatanOnChange()">
					<?php
						foreach ($arrTingkatan as $tingkatan){
							echo "<option value='". $tingkatan["id"] . "'>". $tingkatan["tingkatan_name"] ."</option>";
						} 
					?>
				</select>
			</div>
			<div>
				<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Kelas :</label>
				<select  name="kelas_tahun_ajaran_id" id="kelas_tahun_ajaran_id" onChange="getTotalStudent()" >
					<option value="">[ALL]</option>
					<?php
						foreach ($arrKelasTahunAjaran as $kelasTahunAjaran){
							echo "<option value='". $kelasTahunAjaran->id. "'>". $kelasTahunAjaran->kelas->name ."</option>";
						} 
					?>
				</select>
			</div>
			
			
			<div>
				<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>"> Date :</label>
				<?php
					$this->widget('application.components.widgets.DateTextField', array('name'=>'invoice_date','value'=>$invoice_date));
				?>
			</div>
			<div>
				<label class="form" style="vertical-align:top;text-align:right;width:<?php echo $labelWidth;?>"> Notes :</label>
				<textarea id="notes" name="notes" style="width:650px;height:50px"><?php echo $notes;?></textarea>
			</div>
			<div style="float:right;padding-bottom:0px;margin-bottom:0px">
				<div class="actionBarItem imgBtn" style="float:left;margin-right:15px;" onclick="addDetail()"> 
				<img src="images/action/Actions-list-add-icon.png" height="25" width="25"> 
				<span>Tambah</span>
				</div>
			</div>
			
			<div style="clear:both"></div>
			<table id="tblInvDetail"></table>
			<div id="navTblInvDetail"></div>
			<div style="text-align:right;width:100%;">
					<label style="margin-right:20px;font-weight:bold">Total : </label>
					<label class="form" style="width:100px;margin-right:5px;font-weight:bold" id="total">0</label>
				</div>
				<div style="text-align:right;width:100%;">
					<label style="margin-right:20px;font-weight:bold">Disc : </label>
					<label class="form" style="width:100px;margin-right:5px;font-weight:bold" id="disc">0</label>
				</div>
				<div style="text-align:right;width:100%;">
					<label style="margin-right:20px;font-weight:bold">Total after Disc : </label>
					<label class="form" style="width:100px;margin-right:5px;font-weight:bold" id="totalAfterDisc">0</label>
				</div>
				
				</br>
			<textarea id="invDetail" name="invDetail" style="display:none"></textarea>
		</form>		
	</div>
</div>